package com.xc.quickstart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.quickstart.common.constant.ConfigKeyConstant;
import com.xc.quickstart.common.constant.ConfigTypeConstant;
import com.xc.quickstart.common.context.CommonContext;
import com.xc.quickstart.mapper.CrmAdminMapper;
import com.xc.quickstart.mapper.entity.CrmAdminENT;
import com.xc.quickstart.mapper.entity.CrmConfigENT;
import com.xc.quickstart.service.CrmAdminService;
import com.xc.quickstart.service.CrmConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 团队成员信息表 服务实现类
 * </p>
 *
 * @author code generator
 * @since 2020-12-23
 */
@Service
public class CrmAdminServiceImpl extends ServiceImpl<CrmAdminMapper, CrmAdminENT> implements CrmAdminService {

    @Autowired
    private CrmConfigService crmConfigService;

    @Autowired
    private CrmAdminMapper crmAdminMapper;

    @Override
    public List<String> listCrmDepartmentJobNumber() {
        CrmConfigENT currentENT = crmConfigService.getCurrentENT(ConfigKeyConstant.CRM_DEPARTMENT_ID, ConfigTypeConstant.CRM);

        if (null == currentENT || StringUtils.isEmpty(currentENT.getConfigValue())) {
            log.warn("获取crm部门id为空");
            return Collections.emptyList();
        }

        Integer departmentId = Integer.valueOf(currentENT.getConfigValue().trim());
        List<CrmAdminENT> crmAdminENTList = crmAdminMapper.list(departmentId);

        return crmAdminENTList.stream().map(CrmAdminENT::getJobNumber).collect(Collectors.toList());
    }

    @Override
    public CrmAdminENT get(Integer userId) {
        LambdaQueryWrapper<CrmAdminENT> queryWrapper = Wrappers.<CrmAdminENT>lambdaQuery()
                .eq(CrmAdminENT::getIsValid, CommonContext.VALID_STATUS.ENABLE)
                .eq(CrmAdminENT::getUserId, userId)
                .last("limit 1");

        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public CrmAdminENT get(String jobNumber) {
        LambdaQueryWrapper<CrmAdminENT> queryWrapper = Wrappers.<CrmAdminENT>lambdaQuery()
                .eq(CrmAdminENT::getIsValid, CommonContext.VALID_STATUS.ENABLE)
                .eq(CrmAdminENT::getJobNumber, jobNumber)
                .last("limit 1");

        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public CrmAdminENT getByMobile(String mobile) {
        LambdaQueryWrapper<CrmAdminENT> queryWrapper = Wrappers.<CrmAdminENT>lambdaQuery()
                .eq(CrmAdminENT::getIsValid, CommonContext.VALID_STATUS.ENABLE)
                .eq(CrmAdminENT::getMobile, mobile)
                .last("limit 1");

        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public Set<String> listSendWeChatJobNumber() {
        List<String> crmJobNumberList = listCrmDepartmentJobNumber();
        Set<String> jobNumberSet = new HashSet<>(crmJobNumberList);

        CrmConfigENT currentENT = crmConfigService.getCurrentENT(ConfigKeyConstant.RELEASE_NOTICE_JOBNUM, ConfigTypeConstant.CRM);

        if (null != currentENT && StringUtils.isNotBlank(currentENT.getConfigValue())) {
            List<String> configJobNumberList = Arrays.stream(currentENT.getConfigValue().split(",")).collect(Collectors.toList());
            jobNumberSet.addAll(configJobNumberList);
        }

        if (CollectionUtils.isEmpty(jobNumberSet)) {
            log.warn("[BootCompleteRunner]找不到待发送的工号");
            return Collections.emptySet();
        }

        return jobNumberSet;
    }
}
